package com.zhang.second.day02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @title: flatmap 算子
 * @author: zhang
 * @date: 2022/1/29 19:50
 */
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .fromElements("white", "black", "gray")
                .flatMap(new FlatMapFunction<String, String>() {
                    @Override
                    public void flatMap(String in, Collector<String> out) throws Exception {
                        if (in.equals("white")) {
                            out.collect(in);
                        } else if (in.equals("black")) {
                            out.collect(in);
                            out.collect(in);
                        }
                    }
                })
                .print(">>>>>>");

        env
                .fromElements("white", "black", "gray")
                .flatMap((String in, Collector<String> out) -> {
                    if (in.equals("white")) {
                        out.collect(in);
                    } else if (in.equals("black")) {
                        out.collect(in);
                        out.collect(in);
                    }
                })
                //编译成字节码时，将Collector<String> 类型擦出为Collector<Object>
                //需要做类型注解
                .returns(Types.STRING)
                .print("<<<<<<");

        env.execute();
    }
}
